package com.yjt.gcss.gddw.modules.infoSxxy;

import java.util.Hashtable;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yjt.common.easyui.datagrid.DataGridDTO;
import com.yjt.common.easyui.datagrid.DataGridJsonDTO;
import com.yjt.common.tools.StringUtil;
import com.yjt.common.tools.TimeUtil;
import com.yjt.gcss.gddw.common.bean.domain.InfoSxxy;
import com.yjt.gcss.gddw.common.dao.InfoSxxyDao;

@Service(InfoSxxyService.BEAN_PACKAGE_NAME)
@Transactional(readOnly=true)
public class InfoSxxyService {

	public static final String BEAN_PACKAGE_NAME = "com.yjt.gcss.gddw.modules.infoSxxy.InfoSxxyService";
	
	@Resource(name = InfoSxxyDao.BEAN_PACKAGE_NAME)
	private InfoSxxyDao infoSxxyDao;
	
	public DataGridJsonDTO query(DataGridDTO datagrid, InfoSxxy queryModel) {
		Hashtable<String, String> paramsTable = new Hashtable<String, String>();
		String startDateActive = queryModel.getQuery_start_date_active_date();
		String endDateActive = queryModel.getQuery_end_date_active_date();
		//生效日期
		if(!StringUtil.isEmpty(startDateActive)){
			paramsTable.put("query_start_date_active_date", TimeUtil.changeStrTimeFormat(startDateActive, "yyyy-MM-dd", "yyyyMMdd"));
			infoSxxyDao.enableFilter("infoSxxyFilterByActiveDateStartDate", paramsTable);
			paramsTable.clear();
		}
		if(!StringUtil.isEmpty(endDateActive)){
			paramsTable.put("query_end_date_active_date", TimeUtil.changeStrTimeFormat(endDateActive, "yyyy-MM-dd", "yyyyMMdd"));
			infoSxxyDao.enableFilter("infoSxxyFilterByActiveDateEndDate", paramsTable);
			paramsTable.clear();
		}
		if(!StringUtil.isEmpty(startDateActive) || !StringUtil.isEmpty(startDateActive)) {
			infoSxxyDao.addOrder(Order.desc("activeDate"));
		}
		infoSxxyDao.addOrder(Order.desc("id.userNo"));
		infoSxxyDao.addOrder(Order.desc("id.orgId"));
		infoSxxyDao.addOrder(Order.desc("id.tranType"));
		DataGridJsonDTO result = infoSxxyDao.getBeansByBeanForPager(queryModel, MatchMode.ANYWHERE, datagrid, true);
		if(StringUtils.isNotBlank(startDateActive)) {
			infoSxxyDao.disableFilter("uiDkxyFilterByJYRQStartDate");
		}
		if(StringUtils.isNotBlank(endDateActive)) {
			infoSxxyDao.disableFilter("uiDkxyFilterByJYRQEndDate");
		}
		return result;
	}
}
